﻿using Nasty.Core.Session;
using Nasty.Core;
using Nasty.Core.Entity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nasty.Core.Attributes;
using Newtonsoft.Json;

namespace Nasty.Core.Entity.System
{
    [SugarTable("SysUser", TableDescription = "用户")]
    public class User : StandardEntity<User>
    {
        /// <summary>
        /// 名称
        /// </summary>
        [NastyColumn(ColumnName = "Name", ColumnDescription = "名称", Required = true)]
        public string? Name { get; set; }

        /// <summary>
        /// 手机号码
        /// </summary>
        [SugarColumn(ColumnName = "MobileNumber", IsNullable = true, ColumnDescription = "手机号码")]
        public string? MobileNumber { get; set; }

        /// <summary>
        /// 头像
        /// </summary>
        [SugarColumn(ColumnName = "Avatar", IsNullable = true, ColumnDescription = "头像")]
        public string? Avatar { get; set; }

        /// <summary>
        /// 账号
        /// </summary>
        [NastyColumn(ColumnName = "Account", ColumnDescription = "账号", IsRepeat = true, Required = true)]
        public string? Account { get; set; }

        /// <summary>
        /// 邮箱
        /// </summary>
        [NastyColumn(ColumnName = "Email", ColumnDescription = "邮箱", IsNullable = true)]
        public string? Email { get; set; }

        /// <summary>
        /// 简介
        /// </summary>
        [NastyColumn(ColumnName = "Introduction", ColumnDescription = "简介", IsNullable = true)]
        public string? Introduction { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [NastyColumn(ColumnName = "Password", OnChange = "PasswordChange", ColumnDescription = "密码", Required = true)]
        public string? Password { get; set; }


        /// <summary>
        /// 是否禁用
        /// </summary>
        [SugarColumn(ColumnName = "IsDisabled", ColumnDescription = "是否禁用")]
        public bool IsDisabled { get; set; }

        /// <summary>
        /// 用户角色
        /// </summary>
        [Navigate(typeof(UserRole), nameof(UserRole.UserId), nameof(UserRole.RoleId))]
        public List<Role>? Roles { get; set; }

        /// <summary>
        /// 用户部门
        /// </summary>
        [Navigate(typeof(DepartmentUser), nameof(DepartmentUser.UserId), nameof(DepartmentUser.DepartmentId))]
        public List<Department>? Departments { get; set; }


        /// <summary>
        /// 用户主要部门Id
        /// </summary>
        [SugarColumn(ColumnName = "MainDepartmentId", IsNullable = true, ColumnDescription = "用户主要部门Id")]
        public string? MainDepartmentId { get; set; }

        /// <summary>
        /// 用户主要部门
        /// </summary>
        [Navigate(NavigateType.OneToOne, nameof(User.MainDepartmentId), nameof(User.MainDepartment.Id))]
        public Department? MainDepartment { get; set; }


        private void PasswordChange(object o, object v)
        {
            if (v == null) return;
            if (o == v) return;

            this.Password = Tools.Md5(v.ToString());
        }

        public override void OnPreAdd()
        {
            base.OnPreAdd();

            var db = AppSession.CurrentDb.Value;
            this.IsDisabled = false;
        }


        public override void OnPreUpdate()
        {
            base.OnPreUpdate();
        }


        public override void OnPreDelete()
        {
            base.OnPreDelete();

            var db = AppSession.CurrentDb.Value;

            {
                db.Deleteable<UserRole>().Where((t) => t.UserId == this.Id).ExecuteCommand();
            }

            {
                db.Deleteable<DepartmentUser>().Where((t) => t.UserId == this.Id).ExecuteCommand();
            }
        }
    }
}
